package com.leaf.lowcode.controller;

import com.alibaba.fastjson.JSONObject;
import com.leaf.lowcode.entity.SmogBean;
import com.leaf.lowcode.mapper.XFSmogMapper;
import com.leaf.lowcode.utils.RegularInstance;
import com.tencent.welink.iot.SubDeviceDTO;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 消防用水
 */


@Component
//@RabbitListener(queues="smr3001_queue")
public class SendDataXfSmog {
    @Autowired
    private XFSmogMapper xfSmogMapper;
    @RabbitHandler
    public void sendWDate(JSONObject fire) {
        /**
         * 进入烟感设备
         */
        System.out.println("进入烟感系统");
        System.out.println("fire"+fire);
        String deviceCode= (String) fire.get("deviceCode");
        System.out.println("deviceCode"+deviceCode);
        String dataType= (String) fire.get("dataType");
        System.out.println("dataTpe"+dataType);
        System.out.println( "data==="+fire.get("data"));

        if (dataType.equals("FC_DATA")) {
            Map<Object,Object> data = (HashMap<Object, Object>) fire.get("data");
            System.out.println(data);
            data.get("alarm");
            SmogBean smogBean = new SmogBean();
            smogBean.setMsg(data.get("msg").toString());
            smogBean.setAlarm(data.get("alarm").toString());
            smogBean.setDates(new Date());
            smogBean.setSensorType("na");
            smogBean.setAddress("na");
            xfSmogMapper.insert(smogBean);
        } else if (dataType.equals("TRS_DATA")) {
            Map<Object,Object> data = (HashMap<Object, Object>) fire.get("data");
            System.out.println(data);
            data.get("alarm");
            SmogBean smogBean = new SmogBean();
            smogBean.setMsg(data.get("msg").toString());
            smogBean.setAlarm(data.get("alarm").toString());
            smogBean.setDates(new Date());
            smogBean.setSensorType("na");
            smogBean.setAddress("na");
            xfSmogMapper.insert(smogBean);

        } else if (dataType.equals("FC_SENSOR_DATA")) {
            Map<Object,Object> data = (HashMap<Object, Object>) fire.get("data");
            System.out.println(data);
            data.get("alarm");
            SmogBean smogBean = new SmogBean();
            smogBean.setMsg(data.get("msg").toString());
            smogBean.setAlarm(data.get("alarm").toString());
            smogBean.setDates(new Date());
            smogBean.setSensorType(data.get("sensorType").toString());
            smogBean.setAddress(data.get("address").toString());
            System.out.println(data.get("address").toString());
            xfSmogMapper.insert(smogBean);
        }


        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
        if (subDeviceDTOList != null) {
            for (SubDeviceDTO dto : subDeviceDTOList) {
                String pid = dto.getPid();
                String sn = dto.getSn();
                String din = dto.getDin();
                if (deviceCode.equals(sn)) {
                    System.out.println("找到设备" + sn);
//                    JSONObject standardData = new JSONObject();
//                    standardData.put("reportTs", System.currentTimeMillis());
//                    JSONObject profile = new JSONObject();
//                    profile.put("modelId", 200608);
//                    profile.put("poiCode", "w0307015");
//                    standardData.put("profile", profile);
//                    WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
//                    JSONObject properties = new JSONObject();
//                    JSONObject propertie = new JSONObject();
//                    propertie.put("deviceType", fireCotrolWater.getDeviceType());
//                    propertie.put("indexCode", fireCotrolWater.getDeviceCode());
//                    propertie.put("dataType", fireCotrolWater.getDataType());
//                    /**
//                     * channel1Type=0的时候channel1的值为压力数据；
//                     * channel1Type=1的时候channel1的值为液位数据；
//                     *
//                     * channel2Type=0的时候channel2的值为压力数据；
//                     * channel2Type=1的时候channel2的值为液位数据；
//                     */
//                    propertie.put("channel1", fireCotrolWater.getData().getChannel1());
//                    propertie.put("channel2", fireCotrolWater.getData().getChannel2());
//                    propertie.put("channel1Type", fireCotrolWater.getData().getChannel1Type());
//                    propertie.put("channel2Type", fireCotrolWater.getData().getChannel2Type());
//                    propertie.put("batteryValue", fireCotrolWater.getData().getBatteryValue());
//                    propertie.put("signal", fireCotrolWater.getData().getSignal());
//                    properties.put("smr1210v22", propertie);
//                    standardData.put("properties", properties);
//                    standardData.put("events", new JSONObject());
//                    try {
//                        String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
//                        wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);
//                    } catch (Exception e) {
//                    }
//                }
//            }
//        }

                    System.out.println("fire" + fire);


                }
            }
        }

    }
}